#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define VEC_SZ 20
#define STR_SZ 40
#define FR_SZ 5

//#define DEBUGFR

#define PEGAR_VETOR(F, I, V, P, Q)                     \
  while ((c = fgetc(F)) != EOF) {                      \
    i = 1;                                             \
    if (I == VEC_SZ)                                   \
      break;                                           \
    else if (c == '\n')                                \
      continue;                                        \
    else {                                             \
      V[I][0] = c;                                     \
      while (((c = fgetc(F)) != '\n') && (c != ':') && \
	    (c != EOF)) {                                  \
        if (i == STR_SZ) break;                        \
        else V[I][i++] = c;                            \
      }                                                \
      V[I][i] = '\0';                                  \
	  i = 0;                                           \
	  while (((c = fgetc(F)) != '\n') && (c != EOF)) { \
		if (i == FR_SZ) {                              \
		  fprintf(stderr, "Aumente FR_SZ!\n");         \
		  exit(1);                                     \
		}                                              \
		Q[i++] = c;                                    \
	  }                                                \
	  Q[i] = '\n';                                     \
	  sscanf(Q, "%d", &P[I++]);                        \
    }                                                  \
  }                                                    

int main(int argc, char ** argv) {
  char gab[VEC_SZ][STR_SZ + 1];
  int gabfr[VEC_SZ];
  char tst[VEC_SZ][STR_SZ + 1];
  char frtemp[FR_SZ];
  int tstfr[VEC_SZ];
  FILE * gabf;
  FILE * tstf;
  int gc, tc, i, j, c, prod=0, modgab=0, modtst=0;

  if (argc != 3) {
    fprintf(stderr, "Use: stat <arq_gabarito> <arq_teste>\n");
    exit(1);
  }

  if ((gabf = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Erro de leitura: %s\n", argv[1]);
    exit(1);
  }

  if ((tstf = fopen(argv[2], "r")) == NULL) {
    fprintf(stderr, "Erro de leitura: %s\n", argv[2]);
    (void) fclose(gabf);
    exit(1);
  }

  gc = tc = 0;
  PEGAR_VETOR(gabf, gc, gab, gabfr, frtemp);
  PEGAR_VETOR(tstf, tc, tst, tstfr, frtemp);
  
  #ifdef DEBUGFR
    for (i=0; i<gc; i++) printf("(1) %s : %d\n", gab[i], gabfr[i]);
    printf("-%d\n", tc);
	for (j=0; j<tc; j++) printf("(2) %s : %d\n", tst[j], tstfr[j]);
  #endif
  
  #ifdef DEBUGFR
    for (i=0; i<gc; i++) 
	  for (j=0; j<tc; j++) {
	    if (!strcmp(tst[j], gab[i])) { 
		  printf("%s -> %d : %d\n", gab[i], gabfr[i], tstfr[j]);
		  break;
		}
	  }
  #endif

  fclose(gabf);
  fclose(tstf);
  
  #ifdef DEBUGFREX
  exit(0);
  #endif

  for (j = tc - 1; j >= 0; j--) {
    modtst += tstfr[j] * tstfr[j];
    for (i = gc - 1; i >= 0; i--)
      if (!strcmp(tst[j], gab[i])) {
	    prod += gabfr[i] * tstfr[j];
		break;
	  }
	}
    
  for (j = gc - 1; j >= 0; j--) {
    modgab += gabfr[j] * gabfr[j];
  }

  #ifdef DEBUGFR
  printf("%d %d %d\n", prod, modgab, modtst);
  #endif
  
  printf("%.2f%%\n", (gc) ? (100.0 * prod / (sqrt(modgab * modtst))) : 0.0);

  return 0;
}

